import type { TableInstance } from 'element-plus'

export default function useTableData() {
  const loading = ref<boolean>(false)

  const dataSource = ref<any>(
    Array.from({ length: 6 }, (_, i) => ({
      id: i + 1,
      apply_cert_number: i % 3 === 0 ? '无限制' : '500',
      apply_domain_number: i % 3 === 0 ? '无限制' : '500',
      group: i === 0 ? '普通用户' : `分组${i}`,
      users: i * 20,
      sort: i === 0 ? undefined : 100 - i,
      remark: i === 0 ? '创建用户时的默认分组' : '备注信息XXXXX',
      create_time: 1678418641 + i * 49800
    }))
  )

  const selections = ref([])
  const tableRef = ref<TableInstance>()
  const paginationRef = ref()

  const getTableList = () => {
    loading.value = true
    setTimeout(() => {
      loading.value = false
    }, 1000)
  }

  /** 分页改变事件 */
  const selectionChange = rows => {
    selections.value = rows
    const allCheck = rows.length === dataSource.value.length
    paginationRef.value?.selectionChange(
      allCheck,
      selections.value.length > 0 && !allCheck
    )
  }
  // const filterDataSource = (props: Record<string, any>) => {
  //   console.log(props)
  //   getTableList()
  // }

  onMounted(getTableList)

  return {
    dataSource,
    getTableList,
    selectionChange,
    paginationRef,
    tableRef,
    selections,
    loading
    // filterDataSource
  }
}
